package com.wxapp.admin.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wxapp.admin.api.enums.MenuTypeEnum;
import com.wxapp.admin.api.pojo.entity.MenuDO;
import com.wxapp.admin.api.pojo.entity.RoleDO;
import com.wxapp.admin.api.pojo.entity.RoleMenuDO;
import com.wxapp.admin.api.pojo.entity.UserRoleDO;
import com.wxapp.admin.api.pojo.vo.roleMenu.QAddRoleMenuPerssionVo;
import com.wxapp.admin.api.pojo.vo.roleMenu.QRoleSaveVo;
import com.wxapp.admin.service.mapper.*;
import com.wxapp.admin.service.service.RoleMenuService;
import com.wxapp.common.constant.base.IBaseService;
import com.wxapp.common.constant.pojo.vm.ResultVm;
import com.wxapp.common.constant.pojo.vo.UserVo;
import com.wxapp.common.util.check.CheckUtil;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lgl
 * @Description 权限管理
 * @Datetime 2022/6/6 9:12
 * @Modified By
 */
@Service
@RequiredArgsConstructor
public class RoleMenuServiceImpl extends IBaseService implements RoleMenuService {
    private final UserRoleMapper userRoleMapper;
    private final RoleMapper roleMapper;
    private final RoleMenuMapper roleMenuMapper;
    private final MenuMapper menuMapper;

    @Override
    @GlobalTransactional(rollbackFor = {Exception.class, RuntimeException.class})
    public ResultVm<String> addMeanPerssion(QAddRoleMenuPerssionVo QAddRoleMenuPerssionVo) {
        var createBy = QAddRoleMenuPerssionVo.getUserId();
        LambdaQueryWrapper<UserRoleDO> deptRole = new LambdaQueryWrapper<>();
        deptRole.eq(UserRoleDO::getUserId, QAddRoleMenuPerssionVo.getUserId());
        deptRole.eq(UserRoleDO::getStatus, true);
        var wxappUserDeptRole = userRoleMapper.selectFirst(deptRole);
        if (CheckUtil.isNullObject(wxappUserDeptRole)) {
            RoleDO roleDO = new RoleDO();
            roleDO.setRoleName("");
            roleDO.setRoleSort(1);
            roleDO.setWork(true);
            roleMapper.insert(roleDO);
            wxappUserDeptRole = new UserRoleDO();
            wxappUserDeptRole.setRoleId(roleDO.getId());
            wxappUserDeptRole.setUserId(QAddRoleMenuPerssionVo.getUserId());
            //				 wxappUserDeptRoleDO.setCreateBy(id);
            userRoleMapper.insert(wxappUserDeptRole);

        }
        var url = QAddRoleMenuPerssionVo.getUrl();
        List<RoleMenuDO> roleMenu = new ArrayList<>();
        List<MenuDO> menu = new ArrayList<>();
        url.forEach(s -> {
            MenuDO wxappMenuDO = new MenuDO();
            wxappMenuDO.setPermission(s.replace("/", ":").substring(1, s.length()));
            wxappMenuDO.setBackUrl(s);
            wxappMenuDO.setOrderNum(1);
            wxappMenuDO.setName("");
            wxappMenuDO.setFrame(true);
            wxappMenuDO.setMenuType(MenuTypeEnum.F);
            wxappMenuDO.setDisplay(true);
            menu.add(wxappMenuDO);
        });
        menuMapper.insertBatch(menu);
        var menuId = menu.stream().map(MenuDO::getId).toList();
        for (String s : menuId) {
            RoleMenuDO roleMenuDO = new RoleMenuDO();
            roleMenuDO.setMenuId(s);
            roleMenuDO.setRoleId(wxappUserDeptRole.getRoleId());
            roleMenu.add(roleMenuDO);
        }
        roleMenuMapper.insertBatch(roleMenu);
        return success();
    }

    @Override
    @GlobalTransactional(rollbackFor = {Exception.class, RuntimeException.class})
    public ResultVm<String> addPerssion(UserVo userVo, QRoleSaveVo qRoleSaveVo) {
        var role = roleMapper.selectById(qRoleSaveVo.getRoleId());
        if (CheckUtil.isNullObject(role)) {
            return error("没有找到角色信息");
        }
        LambdaUpdateWrapper<RoleMenuDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(RoleMenuDO::getRoleId, qRoleSaveVo.getRoleId()).eq(RoleMenuDO::getStatus, true);
        roleMenuMapper.delete(wrapper);
        List<RoleMenuDO> roleMenuDOS = new ArrayList<>();
        var menuIdList = qRoleSaveVo.getMenuId();
        for (String s : menuIdList) {
            RoleMenuDO roleMenuDO = new RoleMenuDO();
            roleMenuDO.setMenuId(s);
            roleMenuDO.setRoleId(qRoleSaveVo.getRoleId());
            roleMenuDO.setCreateBy(userVo.getId());
            roleMenuDOS.add(roleMenuDO);
        }
        if (CheckUtil.isNotNullList(roleMenuDOS)) {
            roleMenuMapper.insertBatch(roleMenuDOS);
        }
        return success("添加权限成功");
    }
}
